<script>
Number.prototype.formatMoney = function(decPlaces, thouSeparator, decSeparator) {
    var n = this,
    decPlaces = isNaN(decPlaces = Math.abs(decPlaces)) ? 2 : decPlaces,
    decSeparator = decSeparator == undefined ? "." : decSeparator,
    thouSeparator = thouSeparator == undefined ? "," : thouSeparator,
    sign = n < 0 ? "-" : "",
    i = parseInt(n = Math.abs(+n || 0).toFixed(decPlaces)) + "",
    j = (j = i.length) > 3 ? j % 3 : 0;
    return sign + (j ? i.substr(0, j) + thouSeparator : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thouSeparator) + (decPlaces ? decSeparator + Math.abs(n - i).toFixed(decPlaces).slice(2) : "");
};

function calculate_coin(product_id,product_coin_by_price){
	var quantity = $('select[name=quantity_'+product_id+']').val();
	var coin_calculated = product_coin_by_price*quantity;
	$('#sum_hidden_'+product_id).text(coin_calculated); // Update sum hidden for calculate_total_sum()
	$('#sum_'+product_id).text(coin_calculated.formatMoney(0,'.',','));
	calculate_total_sum();
	return false;
}
function calculate_total_sum(){
	var total = 0;
	$('span.sum_hidden').each(function(){
		sum_coin = parseInt($(this).text());
		total += sum_coin;
	});
	$('#total_sum').text(total.formatMoney(0,'.',','));
}
$(document).ready(function(){
	calculate_total_sum();
});
</script>
<form method="POST" action="<?=PATH_URL.'product/order_submit'?>">
<?php
if(!empty($order_error_message)){
?>
<div style="float:left;width:100%;font-weight:bold;color:red;"><?=$order_error_message?></div>
<?php
}
?>
<div class='order_list'>
	<div class='order_detail order_detail_head'>
		<div class='order_detail_cell order_detail_title'>
		Tên sản phẩm
		</div>
		<div class='order_detail_cell order_detail_price'>
		Giá
		</div>
		<div class='order_detail_cell order_detail_quantity'>
		Số lượng
		</div>
		<div class='order_detail_cell order_detail_sum'>
		Tổng xu
		</div>
	</div>
	<?php
	$order_detail_list = $order['order_detail_list'];
	foreach($order_detail_list as $order_detail){
		$order_detail_product = $order_detail['order_detail_product'];
	?>
	<div class='order_detail order_detail_row'>
		<div class='order_detail_cell order_detail_title'>
			<div class='order_detail_title_text'>
				<?=$order_detail_product['title']?>
			</div>
			<a href="<?=get_detail_url_for_product($order_detail_product)?>" title="<?=$order_detail_product['title']?>">
				<img src="<?=get_image_url_for_product($order_detail_product)?>" />
			</a>
		</div>
		<div class='order_detail_cell order_detail_price'>
		<?=get_price_for_product($order_detail_product)?> <?=MONETARY_UNIT?> (<?=get_coin_by_price_for_product($order_detail_product)?> xu)
		</div>
		<div class='order_detail_cell order_detail_quantity'>
			<select name="quantity_<?=$order_detail_product['id']?>" onchange="return calculate_coin(<?=$order_detail_product['id']?>,<?=get_coin_by_price_for_product($order_detail_product,false)?>)">
			<?php
			for($i=1;$i<=5;$i++){
				$selected = $i==$order_detail['quantity']?'selected="selected"':'';
			?>
			<option value="<?=$i?>" <?=$selected?>><?=$i?></option>
			<?php
			}
			?>
			</select>
		</div>
		<div class='order_detail_cell order_detail_sum'>
			<span id="sum_<?=$order_detail_product['id']?>"><?=get_coin_by_price_for_product($order_detail_product)?></span> xu
			<span class="sum_hidden" id="sum_hidden_<?=$order_detail_product['id']?>" style='display:none;'><?=get_coin_by_price_for_product($order_detail_product,false)?></span>
		</div>
	</div>
	<?php
	}
	?>
</div>
<div class='order_buttons'>
	<div style='float:right;width:161px;'>
		<div style='float:left;width:100%;color:red;font-weight:bold;'>
			Tổng xu thanh toán: <span id="total_sum"></span> xu
		</div>
		<input type="submit" value="MUA" name=""  style='float:left;margin-top:5px;'  />
	</div>
</div>
</form>